<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="org.warehouse.controller.Controller"%>
<%@page import="java.text.MessageFormat"%>
<%@page import="org.warehouse.entity.Group"%>
<%@page import="org.warehouse.entity.Product"%>
<%@ page import="java.util.*"%>
<%@ page import="org.warehouse.controller.Keys"%>
<%@ page import="org.warehouse.util.Sort"%>
<%@ page import="org.warehouse.util.WarehouseConstants"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <div align="center">
        <table style="width: 500px; border: 1px solid grey">
            <tr>
                <td rowspan="2" style="width: 200px; border: 1px solid green">
                    <table title="Groups"
                        style="padding: 5px 0px 5px 5px">
                        <tr>
                            <th>Group Name</th>
                        </tr>

                        <%
                            // Print table of groups
                        	List<Group> groups = (List<Group>) request.getAttribute(Keys.GROUPS_KEY);
                        	// TODO should use some UrlBuilder
                        	String pattern = "<tr><td><a href=\"/warehouse/products?{0}={1}\">{2}</a> ({3})</td></tr>";
                        	for (Group group : groups) {
                        		out.println(MessageFormat.format(pattern, Keys.CURRENT_GROUP_KEY, group.getId(), group.getName(), group.getProductCount()));
                        	}
                        %>
                    </table>
                </td>
                <td style="width: 360px; border: 1px solid green">
                    <%
                    	List<Product> products = (List<Product>) request.getAttribute(Keys.PRODUCTS_KEY);
                    %>
                    <table title="Products" style="padding: 5px 5px 5px 0px; border-color: 1px solid green">
                        <tr>
                            <%
                                // Print names of columns if selected just one group
                                // Names contation an order indicator '^' or 'v'
                            	if (!products.isEmpty()) {
                            		Sort sortByName = Sort.valueOf((String) session.getAttribute(Keys.CURRENT_NAME_SORT_KEY));
                            		Sort sortByPrice = Sort.valueOf((String) session.getAttribute(Keys.CURRENT_PRICE_SORT_KEY));
                            		pattern = "<th><a href=\"/warehouse/products?{0}={1}\">{2} {3}</a></th>";
                            		out.println(MessageFormat.format(pattern, Keys.LAST_SORT_COLUMN_NAME_KEY, "PRODUCT_NAME", "Product Name",
                            		        sortByName == Sort.ASC ? 'v' : '^'));
                            		out.println(MessageFormat.format(pattern, Keys.LAST_SORT_COLUMN_NAME_KEY, "PRODUCT_PRICE", "Product Price",
                            		        sortByPrice == Sort.ASC ? 'v' : '^'));
                            	}
                            %>
                        </tr>

                        <%
                            // Print table of products body
                        	pattern = "<tr><td align=\"center\" style=\"width: 200px\">{0}</td><td align=\"center\" style=\"width: 160px\">{1}</td></tr>";
                        	for (Product product : products) {
                        		out.println(MessageFormat.format(pattern, product.getName(), product.getPrice()));
                        	}
                        %>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="height: 50px; border: 1px solid green" align="center">
                    <div style="padding: 0px 5px 5px 0px">
                        <%
                        	int countOfPages = (Integer) request.getAttribute(Keys.COUNT_OF_PAGES_KEY);
                        	if (countOfPages == 1) {
                        		// Print stub buttons when count of products more then max per screen
                        		out.println("<div align=\"center\" style=\"color: grey\">< 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ></div> ");
                        	} else {
                        		out.println("<div>");
                        		
                                // Print navigation button "<" 
                                pattern = "<a href=\"/warehouse/products?{0}={1}\">{2}</a> ";
                        		out.println(MessageFormat.format(pattern, Keys.PAGE_NAVIGATE_KEY, Keys.PAGE_NAVIGATE_PREV_KEY, "<"));
                                
                                // Print 1, 2, 3.. 
                        		String pattern2 = "<a href=\"/warehouse/products?{0}={1}\">{1}</a>, ";
                        		int startIndex = (Integer) session.getAttribute(Keys.PAGE_START_INDEX_OF_KEY);
                                int maxIndex = startIndex + WarehouseConstants.PAGES_PER_SCREEN;
                                maxIndex = maxIndex > countOfPages ? countOfPages : maxIndex;
                        		for (int i = startIndex; i < maxIndex; i++) {
                        			out.println(MessageFormat.format(pattern2, Keys.CURRENT_PAGE_KEY, i));
                        		}
                                
                        		// Print navigation button ">" 
                                pattern = "<a href=\"/warehouse/products?{0}={1}\">{2}</a> ";
                                out.println(MessageFormat.format(pattern, Keys.PAGE_NAVIGATE_KEY, Keys.PAGE_NAVIGATE_NEXT_KEY, ">"));
                                
                        		out.println("</div>");
                        	}
                        %>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>